package com.cat.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cat.common.*;
import com.cat.mybatis.dao.ImgSourceDao;
import com.cat.mybatis.dao.SpiderConfigDao;
import com.cat.mybatis.domain.ImgSourceDO;
import com.cat.mybatis.domain.SpiderConfigDO;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ？
 *
 * @author xiemh
 * @date 2018/12/12 17:44
 * @since v1.0.0
 */
@Service
public class SpiderService {

    @Resource
    ImgSourceDao imgSourceDao;
    @Resource
    SpiderConfigDao spiderConfigDao;

    public void findImgSourceDO() {
        /**现在只实现百度**/
        List<SpiderConfigDO> list = spiderConfigDao.selectByState(BooleanState.YES.getCode());

        for (SpiderConfigDO sp : list) {
            Integer pageBegin = getImg(sp);
            spiderConfigDao.updateBeginPage(pageBegin, sp.getSpiderId());
        }
    }

    private Integer getImg(SpiderConfigDO sp) {
        Integer pageBegin = sp.getPageBegin();
        for (int i = 0; i < sp.getTimes(); i++) {
            String url = String.format(sp.getUrlFormat(), sp.getWord(), pageBegin, sp.getPageSize());
            saveImgBaidu(url, sp);
            pageBegin += sp.getPageSize();
        }
        return pageBegin;
    }

//    public static void main(String[] args) {
//        String post = JsonStr.post;
////         System.out.println(url);
//        JSONObject json = JSON.parseObject(post);
//
//        JSONArray array = json.getJSONArray("data");
//        // System.out.println(array);
//        List<Object> array2 = array.stream()
//                .filter(j -> ((JSONObject) j).size() > 0 && ((JSONObject) j).get("objURL") != null)
//                .collect(Collectors.toList());
//
//        for (Object j : array2) {
//            JSONObject data = (JSONObject) j;
//            System.out.println(data.get("thumbURL").toString());
//        }
//    }

    private void saveImgBaidu(String url, SpiderConfigDO sp) {
         String post = HttpUtil.post(url, "");
//        String post = JsonStr.post;
        System.out.println(url);
        JSONObject json = JSON.parseObject(post);

        JSONArray array = json.getJSONArray("data");
        if (array == null || array.size() == 0) {
            //爬完了已经，把state改掉
            spiderConfigDao.updateState(BooleanState.NO.getCode(), sp.getSpiderId());
            return;
        }

        List<Object> array2 = array.stream()
                .filter(j -> ((JSONObject) j).size() > 0 && ((JSONObject) j).get("objURL") != null)
                .collect(Collectors.toList());
        // System.out.println(array2.size());

        for (Object j : array2) {
            try {
                JSONObject data = (JSONObject) j;
                ImgSourceDO img = new ImgSourceDO();
                Object objURL = data.get("objURL");
                String srcUrl = SpiderUtils.getBaiduSrcUrl(objURL.toString());
                if (srcUrl.contains("%32")) {
                    //版权限制
                    continue;
                }

                img.setUrl(srcUrl);
                img.setSmallUrl(data.get("thumbURL").toString());
                img.setImgType(data.get("type").toString());
                img.setDomain(data.get("fromURLHost").toString());
                //fromURL
                String md5 = MD5.GetMD5Code(img.getSmallUrl());
                String localPath = SpiderUtils.genPath(sp.getChannel(), sp.getWord(), md5, img.getImgType());
                img.setUrlMd5(md5);
                img.setPath(localPath);
                img.setSmallPath(SpiderUtils.genPathSmall(localPath, img.getSmallUrl()));
                img.setState(ImgState.Init.getCode());
                img.setChannel(sp.getChannel());
                imgSourceDao.insertIgnore(img);
                //TODO batch
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public List<ImgSourceDO> page(){
        List<ImgSourceDO> imgSourceDOs = imgSourceDao.selectPage(0, 100);
        return imgSourceDOs;
    }
}
